package com.eyedsion.his.web.service.sycndata;

import com.eyedsion.his.web.dao.dec.syncdata.DecSyncLogRepository;
import com.eyedsion.his.web.entity.common.DecSyncLog;
import com.eyedsion.his.web.service.JpaBaseService;
import com.eyedsion.his.web.service.businessdata.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author 彭长云
 * @Description:同步业务数据
 * @date 2018/12/13 14:45
 */
@Service
public class BusinessDataService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Value("${syncdata.init}")
    private boolean isInit;
    @Autowired
    private DisDrugBatchDiuknotService disDrugBatchDiuknotService;
    @Autowired
    private IhHospitalizedService ihHospitalizedService;
    @Autowired
    private DisBillStockService disBillStockService;
    @Autowired
    private DisBillStockReturnService disBillStockReturnService;
    @Autowired
    private IhPatientCostService ihPatientCostService;
    @Autowired
    private IhPrepayService ihPrepayService;
    @Autowired
    private IhPresttlPaymentService ihPresttlPaymentService;
    @Autowired
    private IhSettlementService ihSettlementService;
    @Autowired
    private OpPatientCostService opPatientCostService;
    @Autowired
    private OpSettlementRecordService opSettlementRecordService;
    @Autowired
    private OpSettlementPaymentService opSettlementPaymentService;
    @Autowired
    private PatPatientService patPatientService;
    @Autowired
    private RegRegisterService regRegisterService;
    @Autowired
    private OdocVisitRecordService odocVisitRecordService;
    @Autowired
    private OdocDiagnosisInfoService odocDiagnosisInfoService;
    @Autowired
    private DecSyncLogRepository decSyncLogRepository;
    @Autowired
    private OdocDiagnosisDetailService odocDiagnosisDetailService;
    @Autowired
    private InsOpSettlService insOpSettlService;
    @Autowired
    private InsSpodSettlementService insSpodSettlementService;
    @Autowired
    private InsCostLiquidService insCostLiquidService;
    @Autowired
    private InsSettlRbsmtService insSettlRbsmtService;
    @Autowired
    private  CpIhClcpwService cpIhClcpwService;
    @Autowired
    private CpIhClcpwStageService cpIhClcpwStageService;
    @Autowired
    private CommonService commonService;
    private final String lockName = "businessData";

    /**
     * @author： 彭长云
     * @Description:增量更新
     * @date： 2018/12/13 14:50
     * @param： []
     * @return： void
     */
    public DecSyncLog businessDataDs() {
        if(commonService.hasLock(lockName)){
            logger.info("业务数据正在同步，跳过此次同步");
            return new DecSyncLog("业务数据正在同步，跳过此次同步");
        }
        try {
            logger.info("开始同步业务数据。。。");
            long startTime = System.currentTimeMillis();
            //设置默认状态
            JpaBaseService.setSyncMsgMap("",DecSyncLog.SYNC_STATUS_SUCCESS);

            disDrugBatchDiuknotService.incrementUpdate();
            ihHospitalizedService.incrementUpdate();
            disBillStockService.incrementUpdate();
            disBillStockReturnService.incrementUpdate();
            ihPatientCostService.incrementUpdate();
            ihPrepayService.incrementUpdate();
            ihPresttlPaymentService.incrementUpdate();
            ihSettlementService.incrementUpdate();
            opPatientCostService.incrementUpdate(true);
            opSettlementRecordService.incrementUpdate();
            opSettlementPaymentService.incrementUpdate();
            patPatientService.incrementUpdate();
            regRegisterService.incrementUpdate();
            odocVisitRecordService.incrementUpdate();
            odocDiagnosisInfoService.incrementUpdate();
            odocDiagnosisDetailService.incrementUpdate();
            insOpSettlService.incrementUpdate();
            insSpodSettlementService.incrementUpdate();
            insCostLiquidService.incrementUpdate();
            insSettlRbsmtService.incrementUpdate();
            cpIhClcpwService.incrementUpdate();
            cpIhClcpwStageService.incrementUpdate();

            long mi = (System.currentTimeMillis() - startTime) / 1000;
            logger.info("业务数据同步完成,共耗时:" + mi + "s");

        } catch (Exception e) {
            logger.info("基础数据同步出错",e);
        }finally {
            commonService.removeLock(lockName);
            //记录结果到表
            String syncStatus = JpaBaseService.getSyncMsgMap().get(getClass().getName()).getStatus();
            String result = DecSyncLog.SYNC_STATUS_ERROR.equals(syncStatus) ? "同步出现异常" : "同步成功";
            String syncType = isInit ? DecSyncLog.SYNC_TYPE_QL : DecSyncLog.SYNC_TYPE_ZL;
            DecSyncLog decsynclog = decSyncLogRepository.save(
                    new DecSyncLog(DecSyncLog.TYPE_BUSINESSDATA,syncStatus,syncType,"业务数据同步",result ));
            JpaBaseService.getSyncMsgMap().remove(getClass().getName());
            return decsynclog;
        }
    }
}
